package com.knowswift.security.provider;

import com.knowswift.security.AuthUserDetails;
import com.knowswift.security.AuthUserDetailsService;
import com.knowswift.security.account.User;
import com.knowswift.security.param.WxLoginParam;
import com.knowswift.security.token.WeChatAuthenticationToken;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import java.util.Collection;


public class WeChatAuthenticationProvider extends AbstractMatchClassAuthenticationProvider {


    public WeChatAuthenticationProvider(AuthUserDetailsService userDetailsService, Class<? extends WeChatAuthenticationToken> matchClass) {
        super(userDetailsService, matchClass);
    }

    @SneakyThrows
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        WeChatAuthenticationToken weChatAuthenticationToken = (WeChatAuthenticationToken) authentication;
        WxLoginParam wxLoginParam = weChatAuthenticationToken.getWxLoginParam();
        if (wxLoginParam == null || StringUtils.isBlank(wxLoginParam.getCode())) {
            throw new BadCredentialsException("参数错误");
        }
        String userPhone = wxLoginParam.getUserPhone();
        String verifyCode = wxLoginParam.getVerifyCode();
//        String codeInRedis = (String) redisUtils.get(userPhone + "_vc");
//        if (!verifyCode.equals(codeInRedis) && !verifyCode.equals("000000")) {
//            throw new BadCredentialsException("验证码不正确");
//        }
        AuthUserDetails authUserDetails = userDetailsService.loadUserByPhoneNumber(userPhone);
        if (authUserDetails.getId() == null) {
            throw new BadCredentialsException("手机号码未注册，请先登录小程序");
        }
        checkAuthUserDetails(authUserDetails);
//        WechatResponse wechatResponse = WeChatAuthorizeUtils.weChatAuthorization(wxLoginParam.getCode());
//        if (wechatResponse == null || wechatResponse.getErrorCode() != null) {
//            throw new BadCredentialsException("请求异常");
//        }


        User user = (User) authUserDetails;
//        user.setLocked(!wechatResponse.getMiniOpenId().equals(user.getOfficialOpenId()));
//        user.setOfficialOpenId(wechatResponse.getMiniOpenId());
//        AuthUserDetails authUserDetails = userDetailsService.loadUserByOfficialOpenId(wechatResponse.getUnionId(), wechatResponse.getMiniOpenId());
//        User userInfo = WeChatAuthorizeUtils.getUserInfo(wechatResponse.getAccessToken(), wechatResponse.getMiniOpenId());
//        User user = (User) authUserDetails;
//        user.setWechatUnionId(userInfo.getWechatUnionId());
//        user.setUserAvatar(userInfo.getUserAvatar());
        return matchClass.getConstructor(Collection.class, AuthUserDetails.class, WxLoginParam.class)
                .newInstance(authUserDetails.getAuthorities(), authUserDetails, wxLoginParam);
    }
}
